<?php
$sanitize_all_escapes = true;
$fake_register_globals = false;
if (!isset($_REQUEST['medico']))
    session_start();


$landingpage = "index.php?site=" . $_SESSION['site_id'];
if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite'])) {
    $pid = $_SESSION['pid'];
} else {
    if (!isset($_REQUEST['medico'])) {
        session_destroy();
        header('Location: ' . $landingpage . '&w');
        exit;
    }
}
$ignoreAuth = true;
include_once('../interface/globals.php');
include_once('../library/acl.inc');
require_once("$srcdir/patient_provider.inc.php");


function is_actual_encounter(){
    if ( $_SESSION['encounter'] > 0 ){
        $query = " SELECT * "
                . " FROM form_encounter "
                . " WHERE encounter = " .$_SESSION['encounter'] . " AND pid = " . $_SESSION['pid']. " ";
        $result = sqlStatement($query);
        if (sqlNumRows($result) > 0 ){
            $row = sqlFetchArray($result);
            if( $row['closed'] == 0 ){
                return true;
            }else{
                return false;
            }
        }else{
            return false;    
        }
    }else{
        return false;
    }
}
/*
$sql2 ="SELECT   *
        FROM lists l 
	INNER JOIN patient_provider p
           ON l.pid = p.pid
        INNER JOIN users u
           ON u.id = l.provider_id 
       WHERE l.type = 'medical_problem' AND l.provider_id =".$_SESSION[authUserID]." AND l.pid=".$pid;
*/

$sql = "SELECT * FROM lists WHERE pid = ? AND type = 'medical_problem'  ORDER BY begdate";

if (isset($_REQUEST['medico']))
    $pid = $_REQUEST['set_pid'];
$res = sqlStatement($sql, array($pid));
$medico = "";
$medicoIni = "";
if (isset($_REQUEST['medico'])) {
    $medico = "&medico=1&set_pid=" . $pid;
    $medicoIni = "?medico=1&set_pid=" . $pid;
}
$i=0;
//echo $sql2;
//print_r($_SESSION);
echo "<h2>Total Diagn&oacute;sticos";
    $vall = is_actual_encounter();
    if ( $vall ){
        echo  "      <a class='addIcon css_button css_button_left iframe btnNav fancybox.iframe' href='" . $web_root . "/patients/add_edit_issue.php?issue=0&thistype=medical_problem" . $medico . "' title='Agregar' id='agregar_problema'>";
    echo  "      <i class='icon-plus-sign'></i></a>";
    }
    
    echo "<span id='total_problemas' style='float: right; background-color: rgb(228, 228, 228); border-radius: 5px 5px 5px 5px; border-right: 1px solid rgb(204, 204, 204); border-width: 2px 1px 1px; border-style: solid; border-color: -moz-use-text-color rgb(204, 204, 204) rgb(204, 204, 204); -moz-border-top-colors: none; -moz-border-right-colors: none; -moz-border-bottom-colors: none; -moz-border-left-colors: none; border-image: none; padding: 0px 10px;'> " . $i/*sqlNumRows($res)*/ . " </span></h2>";



if (sqlNumRows($res) > 0) {
    $even = false;
    
    echo "<ul>";
    while ($row = sqlFetchArray($res)) {
        $mostrar = true;
        if ($_SESSION['authUserID'] !== $row['provider_id']) {
            $mostrar = existeAutorizacionMedicoInfoPacientes($row['provider_id']);
        }
        
        if ($mostrar) {
            ?>
            <li>
                <p>
                    <span class="title" title='<?php echo htmlspecialchars($row['comments'], ENT_NOQUOTES); ?>'><?php echo htmlspecialchars($row['title'], ENT_NOQUOTES); ?></span>
                    <?php if(acl_check('admin', 'root')){ ?>
                        <a id="borrar<?php echo $row['id'] ?>" class="enlace_borrar iframe_grafico btnNav fancybox.iframe" title="Borrar" href="<?php echo $web_root; ?>/patients/deleter.php?issue=<?php
                            echo $row['id'];
                            echo $medico;
                            ?>">
                            <i class="icon-remove"></i>
                        </a>
                    <?php } ?>
                    <br/>
                    <span class="desc">
                        <?php
                        if ($row['begdate'] != "") {
                            echo htmlspecialchars(toDateView($row['begdate']), ENT_NOQUOTES);
                            if ($row['enddate'] != "")
                                echo " - " . htmlspecialchars(toDateView($row['enddate']), ENT_NOQUOTES);
                            echo "</br>";
                        }
                        else
                            echo "Fecha no registrada</br>";
                        ?>
                    </span>
                </p>
            </li>
            <script type="text/javascript" language="JavaScript">

                $(document).ready(function() {
            <?php
            if ($row['source_entry'] != '1' && !isset($_REQUEST['medico']))
                echo "$('#borrar" . $row['id'] . "').hide();";
            ?>
                    $('#borrar<?php echo $row['id'] ?>').click(function(e) {
                        e.preventDefault();
                        parent.$.fancybox({
                            href: this.href,
                            autoSize: true,
                            type: 'iframe',
                            helpers: {
                                overlay: {
                                    opacity: 0.3,
                                    css: {
                                        'background': 'rgba(255, 254, 254,0.85)'
                                    }
                                } // overlay
                            }, // helpers
                            afterClose: function() {
                                $("#problemasMedicos").load("<?php echo $web_root; ?>/patients/get_issues.php<?php echo $medicoIni; ?>");
                                $("#consulta_medica").load("<?php echo $web_root?>/interface/patient_file/encounter/forms.php?set_encounter=<?php echo $_SESSION['encounter'];?>");
                            }//afterClose
                        }); // fancybox
                    }); // click
                });
                /********************************************************/
            </script>
            <?php
        $i++;
        }
                        
    }
    echo "</ul>";
}
else {
    echo "<ul><li><p>" . htmlspecialchars(xl("No Results"), ENT_NOQUOTES) .
    "</p></li></ul>";
}
?>

<script type="text/javascript" language="JavaScript">
    $(document).ready(function() {
        
        $("#total_problemas").html(<?php echo $i;?>);
        
        $('#agregar_problema').click(function(e) {
            e.preventDefault();
            parent.$.fancybox({
                href: this.href,
                maxWidth: 800,
                maxHeight: 290,
                fitToView: false,
                type: 'iframe',
                iframe: {
                       preload: false // fixes issue with iframe and IE
                },
                helpers: {
                    overlay: {
                        opacity: 0.3,
                        css: {
                            'background': 'rgba(255, 254, 254,0.85)'
                        }
                    } // overlay
                }, // helpers
                afterClose: function() {
                    /*
					$("#problemasMedicos").load("<?php echo $web_root; ?>/patients/get_issues.php<?php echo $medicoIni; ?>", function() {});
                    $("#consulta_medica").load("<?php echo $web_root?>/interface/patient_file/encounter/forms.php?set_encounter=<?php echo $_SESSION['encounter'];?>");
                    */
                }//afterClose
            }); // fancybox
        }); // click
    });
    /********************************************************/
</script>
<!--[if lt IE 7]>
<style type='text/css'>
        #simplemodal-container a.modalCloseImg {
                background:none;
                right:-14px;
                width:22px;
                height:26px;
                filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(
                        src='img/x.png', sizingMethod='scale'
                );
        }
</style>
<![endif]-->	
